/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.   
  * @file     tftlcd.h
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-09
  * @brief    This file contains the interface initialization and basic
  *           control functions of the TFT.
********************************************************************************/ 

#ifndef _TFTLCD_H
#define _TFTLCD_H

#include "module_select.h"
#include "eport_drv.h"
#include "spi_drv.h"
#include "pwm_drv.h"

//----------------------LCD_SPI_Port------------------------------
#define LT_TFTSPI_DMA   2             // 1:DMA  2:EDMA

#if (LT_TFT_PORT==0) 

#define  LCD_RST_RESET  SCI2->SCIPORT &=~(1<<UART_RX)
#define  LCD_RST_SET    SCI2->SCIPORT |= (1<<UART_RX)

#define  LCD_CS_RESET  EPORT->EPDR &=~(1<<1)
#define  LCD_CS_SET    EPORT->EPDR |= (1<<1)

//#define  LCD_RS_CMD	  EPORT->EPDR &=~(1<<0)
//#define  LCD_RS_DATA  EPORT->EPDR |= (1<<0)
#define  LCD_RS_CMD	  SCI1->SCIPORT &=~(1<<UART_RX)//EPORT->EPDR &=~(1<<0)
#define  LCD_RS_DATA  SCI1->SCIPORT |= (1<<UART_RX)//EPORT->EPDR |= (1<<0)

#define  LCD_CLK_RESET  I2C->PDR &= ~(1<<I2C_SCL)
#define  LCD_CLK_SET    I2C->PDR |=  (1<<I2C_SCL)

#define  LCD_SDA_RESET  I2C->PDR &= ~(1<<I2C_SDA)
#define  LCD_SDA_SET    I2C->PDR |=  (1<<I2C_SDA)
#define  LCD_SDA_READ   (I2C->PDR>>=1)

#endif

//----------------------LCD_PPI------------------------------            
#if (LT_TFT_PORT==1||LT_TFT_PORT==2) 

#define  LCD_RST_RESET SCI2->SCIPORT &=~(1<<UART_RX)
#define  LCD_RST_SET   SCI2->SCIPORT |= (1<<UART_RX)

#define  LCD_CS_RESET  EPORT->EPDR &=~(1<<1)
#define  LCD_CS_SET    EPORT->EPDR |= (1<<1)

#define  LCD_RS_CMD    EPORT->EPDR &=~(1<<0)
#define  LCD_RS_DATA   EPORT->EPDR |= (1<<0)

#define  LCD_RD_RESET  SCI2->SCIPORT &=~(1<<UART_TX)
#define  LCD_RD_SET    SCI2->SCIPORT|= (1<<UART_TX)

#define  LCD_WR_RESET  EPORT4->EPDR &=~(1<<6)
#define  LCD_WR_SET    EPORT4->EPDR |= (1<<6)

#define  LCD_WR_READ_L  EPORT4->EPPDR
#define  LCD_WR_READ_H  EPORT4->EPPDR
#define  LCD_WR_WRITE   EPORT4->EPDR
	
#if (LT_TFT_PORT==1) 

//#define  LCD_DATA_WRITE_H  SPI3->SPIPORT           //Write
//#define  LCD_DATA_WRITE_L  SPI1->SPIPORT           //Write
#define  LCD_DATA_WRITE_H(x)  { SPI3->SPIPORT = (x>>4)&0x0F; \
							    SPI1->SPIPORT = (x&0x0F);    }         //Write
#define  LCD_DATA_WRITE_L(x)  { SPI3->SPIPORT = (x>>4)&0x0F; \
							    SPI1->SPIPORT = (x&0x0F);    }         //Write

#define  LCD_DATA_READ_H  ((SPI3->SPIPORT<<4)|SPI1->SPIPORT)           //Read 
#define  LCD_DATA_READ_L  ((SPI3->SPIPORT<<4)|SPI1->SPIPORT)           //Read 

#define  LCD_PIN_OUTPUT  {  SPI3->SPICR1 = 0x10;\
							SPI3->SPICR2 = 0x01; \
							SPI1->SPICR1 = 0x10; \
							SPI1->SPICR2 = 0x01; \
							SPI3->SPIDDR = 0xF;  \
							SPI1->SPIDDR = 0xF;    }
#define  LCD_PIN_INPUT   { 	SPI3->SPIDDR = 0x0;  \
							SPI1->SPIDDR = 0x0;    }
#endif
	
#if (LT_TFT_PORT==2) 

#define  LCD_DATA_WRITE_H  EPORT3->EPDR           //Write
#define  LCD_DATA_WRITE_L  EPORT->EPDR            //Write

#define  LCD_DATA_READ_L  EPORT->EPPDR           //Read

#define LCD_PIN_OUTPUT    \
  {                       \
    EPORT3->EPDDR = 0xff; \
    EPORT->EPDDR = 0xff;  \
  }
#define LCD_PIN_INPUT     \
  {                       \
    EPORT3->EPDDR = 0x00; \
    EPORT->EPDDR = 0x00;  \
  }

#endif

#endif


void LCD_RS(uint8_t bit);
void LCD_CS(uint8_t bit);

//LCD_SPI
void LCD_SPI_Port_Init(void);
uint8_t LCD_SPI_ReadWriteByte(uint8_t Send);
void LCD_SPI_WriteData_DMA(uint8_t *Tx_Addr,uint32_t length);
void LCD_SPI_WriteData_EDMA(uint8_t *Tx_Addr, uint32_t length,uint8_t inc);

//LCD_PPI
void LCD_PPI_Port_Init(void);
void TFT_CommandWrite(uint8_t cmd);
void TFT_DataWrite(uint8_t data);
void TFT_DataRead(uint16_t* buf,uint32_t numpixel) ;
uint32_t TFT_DataReadReg(uint8_t reg,uint8_t NumtoRead);

//LCD_Init
void LCD_TFT_Init(void);
void LT268_TFT_SetWindows(uint16_t XStar,uint16_t yStar,uint16_t Width,uint16_t High);

void ST7789VPanelSleepInMode(void);
void ST7789VPanelSleepOutMode(void);
void LT268_Display_ON(void);
void LT268_Display_OFF(void);

//LCD_pin_test
void LCD_PIN_TEST(void);


#endif	/* _TFTLCD_H */
